//
//  PKforgotpasswordViewController.m
//  pantaKills
//
//  Created by 湛凯杰 on 16/8/3.
//  Copyright © 2016年 湛凯杰. All rights reserved.
//

#import "PKforgotpasswordViewController.h"
#import "PKLoginViewController.h"
@interface PKforgotpasswordViewController ()<UITextFieldDelegate>{
    UIImageView * imageViewOfTitle;
    UITextField * textFieldOfaccount;
    UITextField * textFieldOfpassword;
    UITextField * textFiledOfcode;
    UIButton * btnOfcode;
    UIButton * btnOfsubmit;
    UIButton * btnOfback;
}
@end

@implementation PKforgotpasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    self.view.backgroundColor=MAIN_BACKCOLOR;
    [self addViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - 布局
-(void)addValue{
    if (!imageViewOfTitle) {
        imageViewOfTitle=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"forgotpasword_title"]];
    }
    if (!textFieldOfaccount) {
        textFieldOfaccount=[[UITextField alloc]initTypeOfLonginWithPlaceholder:@"输入手机号" AndSecureTextEntry:YES AndCornerRadius:20];
        textFieldOfaccount.secureTextEntry=NO;
        if (!textFieldOfpassword) {
            textFieldOfpassword=[[UITextField alloc]initTypeOfLonginWithPlaceholder:@"输入新密码" AndSecureTextEntry:YES AndCornerRadius:20];
            textFieldOfpassword.secureTextEntry=YES;
            
        }
    }
    if (!textFiledOfcode) {
        textFiledOfcode=[[UITextField alloc]initTypeOfLonginWithPlaceholder:@"输入验证码" AndSecureTextEntry:YES AndCornerRadius:20];
        textFiledOfcode.secureTextEntry=YES;
    }
    
    if (!btnOfsubmit) {
        btnOfsubmit = [[UIButton alloc]initTypeOfLonginWithTitle:@"Submit" AndCornerRadius:20.0];
        [btnOfsubmit setBackgroundImage:[UIImage imageNamed:@"btn_login"] forState:UIControlStateNormal];
        [btnOfsubmit addTarget:self action:@selector(Subnit) forControlEvents:UIControlEventTouchUpInside];
    }
    if (!btnOfcode) {
        btnOfcode=[[UIButton alloc]initTypeOfLonginWithTitle:@"获取验证码" AndCornerRadius:20.0];
        [btnOfcode setBackgroundImage:[UIImage imageNamed:@"btn_login"] forState:UIControlStateNormal];
        [btnOfcode addTarget:self action:@selector(GetCode) forControlEvents:UIControlEventTouchUpInside];
    }
    if (!btnOfback) {
        btnOfback=[[UIButton alloc]init];
        [btnOfback addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [btnOfback setBackgroundImage:[UIImage imageNamed:@"fullplayer_icon_back"] forState:UIControlStateNormal];
    }
}
-(void)addViews{
    [self addValue];
    [self.view addSubview:imageViewOfTitle];
    [self.view addSubview:textFieldOfpassword];
    [self.view addSubview:textFiledOfcode];
    [self.view addSubview:textFieldOfaccount];
    [self.view addSubview:btnOfsubmit];
    [self.view addSubview:btnOfcode];
    [self.view addSubview:btnOfback];
    
    [imageViewOfTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_top).offset(100);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(71);
    }];
    [textFieldOfaccount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.view.mas_trailing).offset(-30);
        make.leading.equalTo(self.view.mas_leading).offset(30);
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_top).offset(200);
        make.height.mas_equalTo(40);
    }];
    [textFiledOfcode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(textFieldOfaccount);
        make.top.equalTo(textFieldOfaccount.mas_bottom).offset(30);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/2);
    }];
    [btnOfcode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.view.mas_trailing).offset(-30);
        make.top.bottom.equalTo(textFiledOfcode);
        make.leading.equalTo(textFiledOfcode.mas_trailing).offset(30);
    }];
    [textFieldOfpassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(textFieldOfaccount);
        make.top.equalTo(btnOfcode.mas_bottom).offset(30);
        make.height.mas_equalTo(40);
    }];
    [btnOfsubmit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(textFieldOfpassword);
        make.top.equalTo(textFieldOfpassword.mas_bottom).offset(50);
        make.height.mas_equalTo(40);
    }];
    [btnOfback mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(40);
        make.leading.equalTo(self.view.mas_leading).offset(20);
        make.width.height.mas_equalTo(35);
    }];
    
    
}
#define mark - textFdelegate
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [textFiledOfcode resignFirstResponder];
    [textFieldOfaccount resignFirstResponder];
    [textFieldOfpassword resignFirstResponder];
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *info = [aNotification userInfo];
    //取出动画时长
    CGFloat animationDuration=[[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];;
    //取出键盘位置大小
    CGRect keyboardBounds = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect keyboardRect = [self.view convertRect:keyboardBounds toView:nil];
    CGFloat keyboardHeight = keyboardBounds.size.height;
    UIViewAnimationOptions options = (UIViewAnimationOptions)[[info valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
    [UIView animateKeyframesWithDuration:animationDuration delay:0 options:options animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight + 64);
    } completion:nil];
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
    NSDictionary *info = [aNotification userInfo];
    //取出动画时长
    CGFloat animationDuration = [[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    //下移动画options
    UIViewAnimationOptions options = (UIViewAnimationOptions)[[info valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
    //回复动画
    [UIView animateKeyframesWithDuration:animationDuration delay:0 options:options animations:^{
        self.view.transform = CGAffineTransformIdentity;
    } completion:nil];
}

#define mark - 获取验证码 提交 返回
-(void)GetCode{
    [textFiledOfcode resignFirstResponder];
    [textFieldOfaccount resignFirstResponder];
    [textFieldOfpassword resignFirstResponder];
    
}

-(void)Subnit{
    [textFiledOfcode resignFirstResponder];
    [textFieldOfaccount resignFirstResponder];
    [textFieldOfpassword resignFirstResponder];
    
}

-(void)back{
    [textFiledOfcode resignFirstResponder];
    [textFieldOfaccount resignFirstResponder];
    [textFieldOfpassword resignFirstResponder];
    [self presentViewController:[PKLoginViewController new] animated:YES completion:^{
        
    }];
    
}


@end
